#!/bin/ash
#splash window - compatible with yaf-splash
#built upon gtkdialog-splash, written by mave, june 2010.
#other contributors: BarryK, shinobar, 01micko
#gpl license see: /usr/share/doc/legal/gpl-2.0.txt
#Mar 2014, zigbert

[ ! -f /tmp/yaf-splash ] && ln -s "`which gtkdialog`" /tmp/yaf-splash

ROOTGEOM="`xwininfo -root | grep -o ' \\-geometry .*' | cut -f 3 -d ' '`" #ex: 1280x800+0+0
ROOTX=`echo -n "$ROOTGEOM" | cut -f 1 -d 'x'`
ROOTY=`echo -n "$ROOTGEOM" | cut -f 1 -d '+' | cut -f 2 -d 'x'`

MAX_CHAR_WIDTH=40 #Maximum number of chars in one line
MARGIN_DESKTOP=40 #WM bars could conflict if pushing splashes all into the corner

helptext="usage: /path/box_splash [OPTIONS] [-timeout SEC] -text TEXT | -kill PID
  -text \"EXPRESSION\"
  -timeout COUNT (in seconds)
  -icon GTK-XXX (for example: gtk-info all gtk-stock-symbols, default: none)
    OR path/to/pixmap.png|gif|jpeg|svg
  -icon_width WIDTH in pixels
  -bg COLOR (background color red, blue, yellow..., default: grey)
  -bg_gradient true|false (default: true)
  -fg COLOR (font color, default: black)
  -placement center|mouse|top|bottom|top-left|bottom-left (default: center)
  -close never|mouseover|box (default is mouseover)
  -deco TITLE (shows windows decorations, with title)
  -font \"NAME\"
  -fontsize SIZE
  -align ALIGNMENT left, right or center
  -margin SIZE (default: 10)
  -border true|false (default: true)
  -kill PID (process ID called before:
    PID=0 auto-search for the last one,
    PID=xxxxx kill the last one read by PID=\$!"

BasePIDFile="/tmp/GTKDIALOG-SPLASH"

close='mouseover'
text=""
timeout=0
icon=""
font="DejaVu bold"
fontsize="10"
margin=10
border=true
pid=""
placement='center'
deco='false'
title='box_splash'
ICON_WIDTH=32
ALIGN=0 #left
#now, let's see if the default colors is set in config file
[ -s $HOME/.config/ptheme/gtkdialog_active ] && . $HOME/.config/ptheme/gtkdialog_active
[ ! "$BOX_SPLASH_BG" ]	&& BOX_SPLASH_BG=grey
[ ! "$BOX_SPLASH_BG_GRADIENT" ]	&& BOX_SPLASH_BG_GRADIENT=true
[ ! "$BOX_SPLASH_FG" ]	&& BOX_SPLASH_FG=black
bg=$BOX_SPLASH_BG
bg_gradient=$BOX_SPLASH_BG_GRADIENT
fg=$BOX_SPLASH_FG

#parameters
if [ $# = 0 ]; then echo "$helptext"; exit; fi #no parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do 
		case $1 in
			-align)
				case $2 in
					right) ALIGN=1 ;;
					center) ALIGN=2 ;;
					*) ALIGN=0 ;; #left
				esac
				shift
				;;
			-bg)			bg="$2";			shift;;
			-bg_gradient) 	bg_gradient="$2";	shift;;
			-border)
				border="$2"
				[ $border = false ] && notebook_border='show-border="false"'
				shift
				;;
			-close)			close="$2";			shift;;
			-deco)			title="$2";			shift;;
			-fg)			fg="$2";			shift;;
			-font)
				N=$(echo $2 | sed -e 's/^.[^0-9]*//'| cut -b1-2| tr -dc '0-9')
				[ "$N" ] && fontsize=''
				font="$2"
				shift
				;;
			-fontsize)
				fontsize="$2"
				[ $2 = small ] && fontsize="8"
				[ $2 = medium ] && fontsize="10"
				[ $2 = large ] && fontsize="12"
				[ $2 = x-large ] && fontsize="14"
				shift
				;;
			-h|-help|--help) echo "$helptext";	exit;;
			-icon)			ICON="$2";			shift;;
			-icon_width)	ICON_WIDTH="$2";	shift;;
			-kill)
				pid=$2
				if [ ${#pid} -eq -1 ]; then
					dlgPID=`ps|grep -w 'GTKDIALOG_SPLASH'`
					kill $dlgPID 2>/dev/null
				else
					TmpFile=$BasePIDFile${pid}
					dlgPID="`cat $TmpFile`"
					kill $dlgPID 2>/dev/null
					rm -f $TmpFile
					exit
				fi
				shift
				;;
			-margin)		margin="$2"; 		shift;;
			-placement)		placement="$2";		shift;;
			-text)			text="$2";			shift;;
			-timeout)		timeout="$2";		shift;;
		esac
		shift
		I=$(($I+1))
	done
done

#put it here in case command have -icon_width specified after -icon
if [ "$ICON" ]; then
	icon="<pixmap width-request=\"$ICON_WIDTH\" space-expand=\"false\" space-fill=\"false\">
	  <width>$ICON_WIDTH</width>
	  <input file stock=\"$ICON\"></input>
	</pixmap>"
	[ "`echo $ICON | grep '^gtk'`" = "" ] && icon="<pixmap width-request=\"$ICON_WIDTH\" space-expand=\"false\" space-fill=\"false\"> 
	  <width>$ICON_WIDTH</width>
	  <input file>\"$ICON\"</input>
	</pixmap>"
fi

#define height and width of the window 
WIDTH=$(($WIDTH+$margin))
HEIGHT=$(($HEIGHT+$margin))
if [ "$border" = "true" ]; then
	WIDTH=$(($WIDTH+10))
	HEIGHT=$(($HEIGHT+10))
fi
NR_CHARS="`echo "$text" | wc -c`"
NR_LINES=`awk -v VAR1=$NR_CHARS -v VAR2=$MAX_CHAR_WIDTH 'BEGIN{print int((VAR1/VAR2)+1)}'`
[ $NR_CHARS -gt $MAX_CHAR_WIDTH ] && CHAR_WIDTH=$MAX_CHAR_WIDTH || CHAR_WIDTH=$NR_CHARS
TMP=`awk -v VAR1=$CHAR_WIDTH -v VAR2=$fontsize 'BEGIN{print int(VAR1*(VAR2*1.0))}'`
WIDTH=$(($WIDTH+$TMP))
TMP=`awk -v VAR1=$NR_LINES -v VAR2=$fontsize 'BEGIN{print int(VAR1*(VAR2*2.0))}'`
HEIGHT=$(($HEIGHT+$TMP))
[ $NR_LINES -gt 1 ] && HEIGHT=$(($HEIGHT+$fontsize)) #gtkdialog <text> widgets does not has such accurate wrapping as we calculate. It wraps words, not chars
if [ "$icon" ]; then
	WIDTH=$(($WIDTH+$ICON_WIDTH+10))
	[ $HEIGHT -lt 62 ] && HEIGHT=62
fi

#process placement, default is center...
WINPLACE=''
WINTYPE=1
case $placement in
	mouse) GEOMPARAM=''; WINPLACE='window-position="2"'; WINTYPE=2;;
	top) X=$((($ROOTX/2)-($WIDTH/2))); Y=$MARGIN_DESKTOP; WINTYPE=10;;
	top-right) X=$(($ROOTX-$WIDTH-$MARGIN_DESKTOP)); Y=$MARGIN_DESKTOP; WINTYPE=10;;
	top-left) X=$MARGIN_DESKTOP; Y=$MARGIN_DESKTOP; WINTYPE=10;;
	bottom) X=$((($ROOTX/2)-($WIDTH/2))); Y=$(($ROOTY-$HEIGHT-$MARGIN_DESKTOP)); WINTYPE=10;;
	bottom-right) X=$(($ROOTX-$WIDTH-$MARGIN_DESKTOP)); Y=$(($ROOTY-$HEIGHT-$MARGIN_DESKTOP)); WINTYPE=10;;
	bottom-left) X=$MARGIN_DESKTOP; Y=$(($ROOTY-$HEIGHT-$MARGIN_DESKTOP)); WINTYPE=10;;
esac

#set backgound
if [ $bg_gradient = true ]; then
	#build svg-background image
	echo '
	<svg version="1.1" width="1000" height="1000">
	  <defs>
	    <linearGradient id="LG_01" x1="7" y1="80" x2="0" y2="0" gradientUnits="userSpaceOnUse">
	      <stop style="stop-color:'$bg';stop-opacity:1" offset="0" />
	      <stop style="stop-color:'$bg';stop-opacity:0.1" offset="1" />
	    </linearGradient>
	  </defs>
	  <rect style="fill:url(#LG_01);fill-opacity:1" width="1000" height="1000"/>
	</svg>' > /tmp/gtkdialog_splash_bg.svg #full opacity
	echo '
	<svg version="1.1" width="1000" height="1000">
	  <defs>
	    <linearGradient id="LG_01" x1="7" y1="80" x2="0" y2="0" gradientUnits="userSpaceOnUse">
	      <stop style="stop-color:'$bg';stop-opacity:1" offset="0" />
	      <stop style="stop-color:'$bg';stop-opacity:0.1" offset="1" />
	    </linearGradient>
	  </defs>
	  <rect style="fill:url(#LG_01);fill-opacity:0.5;stroke-width:0" width="1000" height="1000"/>
	</svg>' > /tmp/gtkdialog_splash_border.svg #a bit transparent
	if [ $border = false ]; then
		gtk_border='bg_pixmap[NORMAL] = "gtkdialog_splash_bg.svg"'
	else
		gtk_border='bg_pixmap[NORMAL] = "gtkdialog_splash_border.svg"'
		gtk_bg='bg_pixmap[NORMAL] = "gtkdialog_splash_bg.svg"'
	fi
else
	gtk_bg="bg[NORMAL] = \"$bg\""
	gtk_border="bg[NORMAL] = \"$bg\""
fi

#gtk-theme
gtkrc='
pixmap_path "/tmp"
style "gtkdialog-splash"
{
	'"$gtk_border"'
}
class "GtkWindow" style "gtkdialog-splash"

style "notebook"
{
	'"$gtk_bg"'
	xthickness	= '"$(($margin+5))"'
	ythickness	= '$margin'
}
class "GtkNotebook" style "notebook"

style "font"
{
	font_name="'"$font"' '"$fontsize"'"
	text[NORMAL]="'$fg'"
	fg[NORMAL]="'$fg'"
}
class "GtkLabel" style "font"'
echo $gtkrc > /tmp/gtkdialog-splash_gtkrc
export GTK2_RC_FILES=/tmp/gtkdialog-splash_gtkrc

#process close of window, default is mouseover...
CLOSETAG1=''
CLOSETAG2='<action signal="enter-notify-event" type="exit">Exit on mouse-over</action>'
case $close in
	never)
		CLOSETAG2=''
		;;
	box)
		CLOSETAG2=''
		CLOSETAG1='<vbox><button>'"`/usr/lib/gtkdialog/xml_button-icon cancel`"'<action type="exit">Exit on click close-box</action></button></vbox>'
		;;
esac

#gui
export GTKDIALOG_SPLASH='	
<window title="'${title}'" focus-on-map="false" skip-taskbar-hint="true" icon-name="gtk-preferences" resizable="false" decorated="'${deco}'" '${WINPLACE}'>
<vbox>
  <notebook show-tabs="false" '"$notebook_border"' space-expand="true" space-fill="true">
    <hbox>
      <vbox>
        '${icon}'
        <text space-expand="true" space-fill="true"><label>""</label></text>
      </vbox> 
      <text justify="'${ALIGN}'" width-chars="'${CHAR_WIDTH}'" space-expand="true" space-fill="true">
        <label>"'${text}'"</label>
      </text>
      '${CLOSETAG1}'
    </hbox>
  </notebook>
</vbox>
'${CLOSETAG2}'
</window>'

case $WINTYPE in
	2) #mouse
		[ ${timeout} -eq 0 ] && exec /tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH #to keep same pid.
		/tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH &
		;;
	10)
		[ ${timeout} -eq 0 ] && exec /tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH --geometry="$WIDTH"x"$HEIGHT"+"${X}"+"${Y}" #to keep same pid.
		/tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH --geometry="$WIDTH"x"$HEIGHT"+"${X}"+"${Y}" &
		;;
	*) #center
		[ ${timeout} -eq 0 ] && exec /tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH --center #to keep same pid.
		/tmp/yaf-splash --class="gtkdialog-splash" -p GTKDIALOG_SPLASH --center &
		;;
esac
dlgPID=$!

pidPATTERN="^${dlgPID} "
while [ $timeout -ne 0 ];do #100604
	sleep 1
	ALLPS="`ps`"
	timeout=`expr $timeout - 1`
	[ "`echo "$ALLPS" | sed -e 's%^ *%%' | grep "$pidPATTERN"`" == "" ] && exit #already killed.
done
kill $dlgPID
echo 'EXIT="Exit on timeout"'
